What is @csstools/css-color-parser?
@csstools/css-color-parser is an npm package designed to parse CSS color values. It can handle a variety of color formats including named colors, hex codes, RGB, RGBA, HSL, and HSLA. This package is useful for developers who need to manipulate or validate CSS color values in their JavaScript applications.
What are @csstools/css-color-parser's main functionalities?
Parse Named Colors
This feature allows you to parse named CSS colors. The code sample demonstrates how to parse the named color 'red' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('red');
console.log(color);
Parse Hex Colors
This feature allows you to parse hex color codes. The code sample demonstrates how to parse the hex color '#ff0000' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('#ff0000');
console.log(color);
Parse RGB Colors
This feature allows you to parse RGB color values. The code sample demonstrates how to parse the RGB color 'rgb(255, 0, 0)' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('rgb(255, 0, 0)');
console.log(color);
Parse RGBA Colors
This feature allows you to parse RGBA color values. The code sample demonstrates how to parse the RGBA color 'rgba(255, 0, 0, 0.5)' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('rgba(255, 0, 0, 0.5)');
console.log(color);
Parse HSL Colors
This feature allows you to parse HSL color values. The code sample demonstrates how to parse the HSL color 'hsl(0, 100%, 50%)' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('hsl(0, 100%, 50%)');
console.log(color);
Parse HSLA Colors
This feature allows you to parse HSLA color values. The code sample demonstrates how to parse the HSLA color 'hsla(0, 100%, 50%, 0.5)' and log the resulting color object.
const { parse } = require('@csstools/css-color-parser');
const color = parse('hsla(0, 100%, 50%, 0.5)');
console.log(color);
Other packages similar to @csstools/css-color-parser
color
The 'color' package is a comprehensive library for color conversion and manipulation. It supports a wide range of color formats and provides methods for color transformations. Compared to @csstools/css-color-parser, 'color' offers more extensive functionality for color manipulation beyond just parsing.
tinycolor2
The 'tinycolor2' package is a fast and lightweight color manipulation library. It supports parsing, conversion, and manipulation of various color formats. While @csstools/css-color-parser focuses on parsing, 'tinycolor2' provides additional methods for color adjustments and transformations.
chroma-js
The 'chroma-js' package is a powerful library for color conversions and color scales. It supports a wide range of color formats and provides advanced features for color manipulation and visualization. Compared to @csstools/css-color-parser, 'chroma-js' offers more advanced capabilities for working with color scales and gradients.
CSS Color Parser
![Discord](https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white)
Usage
Add CSS Color Parser to your project:
npm install @csstools/css-color-parser @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev
import { color } from '@csstools/css-color-parser';
import { isFunctionNode, parseComponentValue } from '@csstools/css-parser-algorithms';
import { serializeRGB } from '@csstools/css-color-parser';
import { tokenize } from '@csstools/css-tokenizer';
const hwbComponentValue = parseComponentValue(tokenize({ css: 'hwb(10deg 10% 20%)' }));
const colorData = color(hwbComponentValue);
if (colorData) {
console.log(colorData);
const rgbComponentValue = serializeRGB(colorData);
console.log(rgbComponentValue.toString());
}